<?php
namespace App\Http\Controllers\Client\Shop\Explore;

use App\Http\Controllers\ClientController;
use App\Models\Tools\AliOss;
use App\Models\Tools\Data;
use Illuminate\Support\Facades\DB;
use Illuminate\Support\Facades\Log;

class ExploreBuyListController extends ClientController
{

    public function index()
    {
        try{

            // 查询是否已经绑定了手机号
            $phone = DB::table('lottery_users')->where('we_id','=',$this->we_id)->value('phone');
            $verified_phone = $phone && true;

            // 查询是否已经绑定了店铺
            $shops = DB::table('shop')->where('we_id','=',$this->we_id)->count();
            $shops = $shops && true;

            // 查询剩余单量
            $channels = DB::table('explore_remaining')
                ->where('we_id','=',$this->we_id)
                ->select('channel_id','remaining_times')
                ->get()
                ->keyBy('channel_id')
                ->map(function($item){
                    return $item->remaining_times;
                })
                ->toArray();

            // 购买条件
            $explore_channels_buy = DB::table('explore_channel')
                ->select('channel_id as id','channel_name as name')
                ->get()
                ->toArray();
            // 购买条件
            $explore_channels_list = DB::table('explore_channel')
                ->select('channel_id as id','price','channel_id as s1')
                ->get()
                ->map(function ($item) use ($channels){
                    $item->stock_num = 10000;
                    $item->remaining = $channels[$item->id]?? 0;
                    return $item;
                })
                ->toArray();

            // 店铺审核状态
            $verified_shop = DB::table('shop')
                ->where('we_id','=',$this->we_id)
                ->where('verified','=',Data::DATA_DB_LAIFEI_SHOP_VERIFIED_SUCCESS)
                ->count();
            $verified_shop = $verified_shop && true;

            return $this->setReturnJson(0,'success',[
                'verified_phone'=>$verified_phone,
                'verified_shop'=>$verified_shop,
                'shops'=>$shops,
                'explore_channels_buy'=>$explore_channels_buy,
                'explore_channels_list'=>$explore_channels_list,
            ]);
        }catch (\Exception $e){
            Log::error(ExploreBuyListController::class, ['msg' => $e->getMessage(), 'file' => $e->getFile(), 'line' => $e->getLine()]);
            return $this->setReturnJson(-1,'查询失败',['msg' => $e->getMessage(), 'file' => $e->getFile(), 'line' => $e->getLine()]);
        }

    }

}
